package com.mlnx.netty.app.ecg.session.manager;

import com.mlnx.netty.app.ecg.session.core.DeviceSession;
import com.mlnx.netty.app.ecg.session.core.MemmerySessionManager;
import com.mlnx.netty.app.ecg.session.core.Session;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import io.netty.channel.Channel;

/**
 * Created by amanda.shan on 2019/12/2.
 */
public class MutilDeviceSessionManager extends MemmerySessionManager {

//    protected List<Session> ecgSessions = new ArrayList<>();

    @Override
    public synchronized void addSession(Session session) {
        super.addSession(session);

//        if (session instanceof EcgDeviceSession) {
//            ecgSessions.add(session);
//        }
    }

    @Override
    public synchronized void removeSession(Session session) {
        super.removeSession(session);
//        ecgSessions.remove(session);
//        if (session instanceof EcgDeviceSession) {
//            DeviceStateRecord deviceStateRecord = new DeviceStateRecord();
//            deviceStateRecord.setDeviceId(((EcgDeviceSession) session).getDeviceId());
//            deviceStateRecord.setDeviceState(DeviceState.DEVICE_OFFLINE);
//            deviceStateRecord.setPatientId(((EcgDeviceSession) session).getPatientId());
//            deviceStateRecord.setDate(new Date());
//            deviceStateService.saveDeviceState(deviceStateRecord);
//        }
    }

    @Override
    public void removeSession(String clientId) {
        Session session = getSession(clientId);
        if (session != null) {
            removeSession(session);
        } else {
            log.info("{} 设备没有在线的session", clientId);
        }
    }

    public Channel getChannel(String deviceId) {
        DeviceSession session = (DeviceSession) getSession(deviceId);
        if (session == null) {
            return null;
        } else {
            return session.getChannel();
        }
    }

//    /**
//     * 根据病人ID和设备类型获取session
//     *
//     * @param patientId
//     * @param deviceType
//     * @return
//     */
//    public Session getSession(Integer patientId, DeviceType deviceType) {
//
//        for (Session session : getSessionClientIdMap().values()) {
//            DeviceSession deviceSession = (DeviceSession) session;
//            switch (deviceType) {
//                case ECG_DEVICE:
//                    if (deviceSession instanceof EcgDeviceSession && deviceSession.getPatientId().equals(patientId)) {
//                        return session;
//                    }
//                    break;
//                case MP_DEVICE:
//                    if (deviceSession instanceof MpDeviceSession && deviceSession.getPatientId().equals(patientId)) {
//                        return session;
//                    }
//                    break;
//                default:
//
//            }
//        }
//        return null;
//    }
//
//    public Collection<String> getEcgDeviceIds() {
//        List<String> ecgDeviceIds =
//                ecgSessions.stream().filter(session -> session instanceof EcgDeviceSession)
//                        .map(session -> session.getClientId()).collect(Collectors.toList());
//        return ecgDeviceIds;
//    }

    public Collection<String> getOnlineDeviceIds(Collection<String> askDeviceIds) {
        List<String> list =
                getSessionChannelMap().values().stream()
                        .filter(session -> askDeviceIds.contains(session.getClientId()))
                        .map(session -> session.getClientId()).collect(Collectors.toList());
        return list;
    }

    public void refreshLastTime(Channel channel) {
        Session session = getSessionChannelMap().get(channel);
        if (session != null) {
            session.setLastPacketTime(new Date());
        }
    }

    @Override
    public String getName() {
        return "MutilDeviceSessionManager";
    }
}
